package com.summer.dao;

import com.summer.bean.Course;
import com.summer.bean.Lecturer;
import com.summer.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Description:
 * User: 35391
 * Package: com.summer.dao
 * Date: 2025/7/22
 * Time: 下午9:56
 * Version: 1.0
 */
public class LecturerDao {
    public static List<Lecturer> getLecturerDetailByTno(String Tno){
        ArrayList<Lecturer> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from lecturer where tno = ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1, Tno);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Lecturer(rs.getString("tno"), rs.getString("tname"), rs.getString("department"), rs.getString("address"), rs.getString("telephone"), rs.getString("title")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return list;
    }
    public static List<Lecturer> getLecturerDetailByLikeTno(String name){
        ArrayList<Lecturer> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from lecturer where tno like ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "%" + name + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Lecturer(rs.getString("tno"), rs.getString("tname"), rs.getString("department"), rs.getString("address"), rs.getString("telephone"), rs.getString("title")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return list;
    }

    public static List<Lecturer> getAllLecturer(){
        ArrayList<Lecturer> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from lecturer;";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Lecturer(rs.getString("tno"), rs.getString("tname"), rs.getString("department"), rs.getString("address"), rs.getString("telephone"), rs.getString("title")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return list;
    }
}
